<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织机构" name="first" />
        </el-tabs>
        <div style="padding:30px 80px">
          <el-row style="border-bottom:1px solid #cfcfcf;padding-bottom:15px">
            <el-col :span="20"><span style="font-weight:700">江苏传智播客教育科技股份有限公司</span></el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-col>负责人</el-col>
                <el-col>
                  <el-dropdown>
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item>添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-tree :data="departList" :default-expand-all="true">
            <template v-slot="{data}">
              <el-row type="flex" justify="space-between" align="middle" style="width: 100%;">
                <el-col :span="20">{{ data.name }}</el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <el-col>{{ data.manager }}</el-col>
                    <el-col>
                      <el-dropdown>
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="Adepart(data.id)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="Adepart(data.id,edit=true)">编辑部门</el-dropdown-item>
                          <el-dropdown-item @click.native="Rdepart(data.id)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </div>
      </el-card>
      <el-dialog
        title="编辑部门"
        :visible.sync="dialogVisible"
        width="50%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <dep-dialog v-if="dialogVisible" :id="id" :is-edit="isEdit" @success="success" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, removeDepartment } from '@/api/department'
import { arrayAsTree } from '@/utils/tree'
import depDialog from './departDialog.vue'
export default {
  components: {
    depDialog
  },
  data() {
    return {
      activeName: 'first',
      departList: [],
      dialogVisible: false,
      id: '',
      isEdit: false
    }
  },
  mounted() {
    this.getDepartList()
  },
  methods: {
    async  getDepartList() {
      const { data: { depts: res }} = await getDepartments()
      this.departList = arrayAsTree(res, '')
    },
    Adepart(id, edit) {
      this.dialogVisible = true
      this.id = id
      if (edit) {
        console.log(edit)
        this.isEdit = true
      } else {
        this.isEdit = false
      }
    },
    Rdepart(id) {
      this.$confirm('是否删除该部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await removeDepartment(id)
        if (res.code === 10000) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDepartList()
        } else {
          this.$message({
            type: 'error',
            message: '删除失败，请联系管理员！'
          })
        }
      }).catch(err => err)
    },
    success() {
      this.dialogVisible = false
      this.id = ''
      //   刷新页面
      this.getDepartList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-tree{
   ::v-deep.el-tree-node{
      padding: 5px 0 !important;
    }

}
.el-dialog__wrapper{
    ::v-deep.el-dialog__header{
        background-color: #66b1ff ;
        color: #fff !important;
    }
}
</style>
